import {
	getMediaInfoi,
} from '@/api/service.js'

import {
	getSnowToken,
	getUserData,
	getToken
} from '@/utils/storage.js'

const env = require('./config')

//上传图片到oss
function upload(filePath, flag, callBack = () => {}) {
	uni.uploadFile({
		url:  env.baseURL + '/applet/miniProgram/register/upload',
		name: 'file',
		filePath: filePath,
		header: {
			accessToken: 'Bearer ' + getToken('access_token_key'),
			Authorization: 'Bearer ' + getToken('access_token_key'),
			'Content-Type': 'multipart/form-data'
		},
		formData: {
			snowFieldId: getSnowToken('snow_field'),
		},
		success: (res) => {
			const msg = JSON.parse(res.data).msg
			const mediaId = JSON.parse(msg).mediaId
			// console.log('mediaId-------: ', mediaId)
			getMediaInfoi({
				Action: 'GetMediaInfo',
				userId: getUserData().id,
				mediaId: mediaId,
			}).then(response => {
				// console.log('response----------: ', response)
				let ossFileName = response.GetMediaInfoResult.MediaInfo
					.FileInfoList[0].FileBasicInfo.FileUrl
				let data = {
					mediaId: mediaId,
					ossFileName: ossFileName,
				}
				callBack(data)
			})
		}
	})
}

export default {
	upload
}